package cn.wonderland.org.controller;

import cn.wonderland.base.annotation.PreAuthorize;
import cn.wonderland.base.jwt.UserInfo;
import cn.wonderland.base.util.AjaxResult;
import cn.wonderland.base.util.LoginContext;
import cn.wonderland.base.util.PageList;
import cn.wonderland.org.domain.Employee;
import cn.wonderland.org.query.EmployeeQuery;
import cn.wonderland.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

//    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
//    public List<Employee> findAll(){
//        return employeeService.findAll();
//    }

    @GetMapping()
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        if(employee.getId() != null){ // id不为空，执行修改
            employeeService.update(employee);
        }else{ // id为空，执行新增
            employeeService.add(employee);
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 分页 json格式交互 字符串
     *
     * @RequestBody 接收前端的json数据 转成我们的实体
     */
    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery quey, HttpServletRequest request) {
        UserInfo userInfo = LoginContext.getUserInfo();
        if (null != userInfo.getShop()) {
            quey.setShopId(userInfo.getShop().getId());
        }
        return employeeService.queryByPage(quey);
    }

    // 批量删除
    @PatchMapping
    @PreAuthorize(name="员工批量删除",sn = "emp:batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        employeeService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    // 根据部门id查询员工集合
    @GetMapping("/findByDeptId/{deptId}")
    public List<Employee> findByDeptId(@PathVariable("deptId") Long dept){
        return employeeService.findByDeptId(dept);
    }


    /**
     * 查询店铺下的员工
     */
    @GetMapping("/list/{shopId}")
    public List<Employee> findListByShopId(@PathVariable("shopId") Long shopId) {
        return employeeService.findListByShopId(shopId);
    }
}